package com.jwb.demo002.bean;

import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import javax.annotation.Resource;

@Configurable
@ComponentScan("com.jwb.demo002.bean")
public class Config {
    public String home;
    public String username;
    private Bean1 bean1;
    @Resource
    private Bean2 bean2;
    private Bean3 bean3;

    @Autowired
    public void setHome(@Value("${MAVEN_HOME}") String home) {
        System.out.println("注入了home：" + home);
        this.home = home;
    }

    @Autowired
    public void setMyUserName(@Value("${my.user.name}") String username) {
        System.out.println("注入了用户名：" + username);
        this.username = username;
    }


    @Bean
    public Bean1 bean1() {
        System.out.println("注入bea1");
        Bean1 bean1 = new Bean1();
        this.bean1 = bean1;
        return bean1;
    }

    @Bean
    public Bean3 bean3(Bean1 bean1) {
        System.out.println("注入Bean3");
        Bean3 bean3 = new Bean3(bean1);
        this.bean3 = bean3;
        return bean3;
    }

    public Bean1 getBean1() {
        return bean1;
    }

    public Bean2 getBean2() {
        return bean2;
    }

    public Bean3 getBean3() {
        return bean3;
    }

    @Override
    public String toString() {
        String jsonStr = JSON.toJSONString(this);
        return jsonStr;
    }
}
